#!/bin/bash
echo 生成系统服务配置
echo 获取当前安装目录
work_path=$(dirname $(readlink -f $0))
#获取安装服务名称 如果未提供服务名称 ，取当前最后一级目录名称为服务名称
if [ x$1 == x ]
then
  echo 请输入服务名称
  echo ./install.sh 服务名称
  return ;

fi

#  project_path=$(cd `dirname $0`; pwd)
#  service_name="${project_path##*/}"
service_name=$1
cat  > /lib/systemd/system/${service_name}.service << EOF
[Unit]
Description=$1
Documentation=
Before=
After=
Wants=
#Request=

[Service]
Type=forking
WorkingDirectory=${work_path}
PermissionsStartOnly=true
ExecStart=${work_path}/start.sh

KillMode=process
ExecStop=${work_path}/stop.sh
PrivateTmp=true

Restart=always
RestartSec=2s

LimitCORE=infinity

[Install]
WantedBy=multi-user.target
EOF
echo 检查配置
echo 设置开机启动
systemctl enable ${service_name}
echo 保存服务名称
echo ${service_name} > app.service_name